package com.example.cmh.controller;

import com.example.cmh.common.Result;
import com.example.cmh.pojo.User;
import com.example.cmh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 分页条件搜索用户信息
     * @param page 页码
     * @param size 每页大小
     * @param userName 用户名
     * @param userPhone 用户电话
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param userStatus 用户状态
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result getUsers(@RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "10") Integer size,
                          @RequestParam(name = "userName", required = false) String userName,
                          @RequestParam(name = "userPhone", required = false) String userPhone,
                          @RequestParam(name = "startTime", required = false) String startTime,
                          @RequestParam(name = "endTime", required = false) String endTime,
                          @RequestParam(name = "userStatus", required = false) String userStatus) {
        Map<String, Object> result = userService.getUsers(page, size, userName, userPhone, startTime, endTime, userStatus);
        return Result.success("获取用户列表成功", result);
    }
    
    /**
     * 根据userId搜索用户信息
     * @param page 页码
     * @param size 每页大小
     * @param userId 用户ID
     * @return 分页结果
     */
    @GetMapping("/byUserId")
    public Result getUsersByUserId(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                  @RequestParam(name = "size", defaultValue = "10") Integer size,
                                  @RequestParam(name = "userId") Integer userId) {
        Map<String, Object> result = userService.getUsersByUserId(page, size, userId);
        return Result.success("获取用户列表成功", result);
    }
    
    /**
     * 根据部门搜索用户信息
     * @param page 页码
     * @param size 每页大小
     * @param departmentId 部门ID
     * @return 分页结果
     */
    @GetMapping("/byDepartment")
    public Result getUsersByDepartmentId(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                        @RequestParam(name = "size", defaultValue = "10") Integer size,
                                        @RequestParam(name = "departmentId") Integer departmentId) {
        Map<String, Object> result = userService.getUsersByDepartmentId(page, size, departmentId);
        return Result.success("获取用户列表成功", result);
    }
    
    /**
     * 新增用户
     * @param user 用户信息
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result addUsers(@RequestBody User user) {
        userService.addUsers(user);
        return Result.success("新增用户成功");
    }
    
    /**
     * 更新用户信息
     * @param userId 用户ID
     * @param user 用户信息
     * @return 操作结果
     */
    @PutMapping("/{userId}")
    public Result updateUser(@PathVariable(name = "userId") Integer userId, @RequestBody User user) {
        user.setUserId(userId);
        userService.updateUser(user);
        return Result.success("更新成功");
    }
    
    /**
     * 删除用户
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/{userId}")
    public Result deleteUser(@PathVariable(name = "userId") Integer userId) {
        userService.deleteUser(userId);
        return Result.success("删除成功");
    }
    
    /**
     * 用户登录
     * @param loginRequest 登录请求
     * @return 用户信息
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> loginRequest) {
        try {
            com.example.cmh.vo.UserVO user = userService.login(loginRequest);
            return Result.success("登录成功", user);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 用户登出
     * @param userId 用户ID
     * @return 操作结果
     */
    @PostMapping("/logout")
    public Result logout(@RequestParam(name = "userId") Integer userId) {
        userService.logout(userId);
        return Result.success("登出成功");
    }
    
    /**
     * 根据多个部门ID分页查询用户信息
     * @param page 页码
     * @param size 每页大小
     * @param departmentIds 部门ID集合
     * @return 分页结果
     */
    @PostMapping("/byDepartmentIds")
    public Result getUsersByDepartmentIds(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                          @RequestParam(name = "size", defaultValue = "10") Integer size,
                                          @RequestBody java.util.List<Integer> departmentIds) {
        java.util.Map<String, Object> result = userService.getUsersByDepartmentIds(page, size, departmentIds);
        return Result.success("获取用户列表成功", result);
    }
    
    /**
     * 用户修改自己的信息
     * @param userId 用户ID
     * @param user 用户信息
     * @return 更新后的用户详细信息
     */
    @PutMapping("/own/{userId}")
    public Result updateOwnUser(@PathVariable(name = "userId") Integer userId, @RequestBody User user) {
        User updated = userService.updateOwnUser(userId, user);
        return Result.success("更新成功", updated);
    }
    
    /**
     * 用户修改密码
     * @param param 包含userId、oldPassword、newPassword
     * @return 操作结果
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody java.util.Map<String, String> param) {
        Integer userId = Integer.valueOf(param.get("userId"));
        String oldPassword = param.get("oldPassword");
        String newPassword = param.get("newPassword");
        userService.updatePassword(userId, oldPassword, newPassword);
        return Result.success("密码修改成功");
    }
} 